package ru.elifantiev.cityrouter.infrastructure.map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import java.util.HashMap;

public class ViewCache {

    private HashMap<Integer, View> cache;
    private LayoutInflater inflater;

    public ViewCache(Context ctx) {
        cache = new HashMap<Integer, View>();
        inflater = LayoutInflater.from(ctx);
    }

    public View getCachedView(int resourceId) {
        if(cache.containsKey(resourceId))
            return cache.get(resourceId);
        else {
            View v = inflater.inflate(resourceId, null);
            cache.put(resourceId, v);
            return v;
        }
    }

    public void destroy() {
        cache.clear();
        inflater = null;
    }
}
